package com.lw.nio.buffer;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author LW
 * @2021/9/13 20:32
 */
public class BufferDemo {

    /**
     * 读取文件内容（乱码未解决）
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        RandomAccessFile file = new RandomAccessFile("C:\\Users\\ASUS\\Desktop\\gdmp.txt", "rw");

        FileChannel channel = file.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(48);

        int read = channel.read(buf);

        while (read != -1) {
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.println((char)buf.get());
            }
            buf.clear();
            read = channel.read(buf);

        }
        file.close();
    }
}
